<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>真性近视筛查系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        h1 {
            color: #2c3e50;
            text-align: center;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 150px;
            font-weight: bold;
        }
        input, select {
            padding: 8px;
            width: 200px;
        }
        button {
            background-color: #3498db;
            color: white;
            padding: 10px 15px;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #2980b9;
        }
        #result {
            margin-top: 20px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            display: none;
        }
    </style>
</head>
<body>
    <h1>真性近视筛查系统</h1>
    
    <div class="form-group">
        <label for="age">年龄(3-18岁):</label>
        <input type="number" id="age" min="3" max="18" step="1">
    </div>
    
    <div class="form-group">
        <label for="gender">性别(Gender):</label>
        <select id="gender" style="width: 200px; padding: 8px;">
            <option value="1">男性</option>
            <option value="0">女性</option>
        </select>
    </div>
    
    <div class="form-group">
        <label for="al">眼轴长度(AL):</label>
        <input type="number" id="al" min="0" step="any">
    </div>
    
    <div class="form-group">
        <label for="cr">角膜曲率(CR):</label>
        <input type="number" id="cr" min="0" step="any">
    </div>
    
    <div class="form-group">
        <label for="al_cr">AL/CR值:</label>
        <input type="number" id="al_cr" readonly>
    </div>
    
    <button onclick="calculateAndPredict()">预测</button>
    
    <div id="result"></div>
    <div id="errorLog" style="display: none; margin-top: 20px; padding: 15px; background-color: #f8d7da; border: 1px solid #f5c6cb; border-radius: 5px;">
        <h3>错误日志</h3>
        <pre id="errorContent"></pre>
    </div>

    <!-- 移除joblib-js的引用 -->
    <script>
        // 计算AL/CR值
        document.getElementById('al').addEventListener('input', calculateALCR);
        document.getElementById('cr').addEventListener('input', calculateALCR);
        
        function calculateALCR() {
            const al = parseFloat(document.getElementById('al').value);
            const cr = parseFloat(document.getElementById('cr').value);
            
            if (al && cr) {
                const al_cr = (al / cr).toFixed(2);
                document.getElementById('al_cr').value = al_cr;
                console.log(`计算AL/CR值: AL=${al}, CR=${cr}, AL/CR=${al_cr}`);
            }
        }
        
        async function calculateAndPredict() {
            console.log('开始预测...');
            // 获取输入值
            const age = parseFloat(document.getElementById('age').value);
            const gender = parseInt(document.getElementById('gender').value);
            const al = parseFloat(document.getElementById('al').value);
            const cr = parseFloat(document.getElementById('cr').value);
            const al_cr = parseFloat(document.getElementById('al_cr').value);
            
            console.log(`输入参数: Age=${age}, Gender=${gender}, AL=${al}, CR=${cr}, AL/CR=${al_cr}`);
            
            if (!age || !al || !cr) {
                console.warn('警告: 请填写所有必填字段！');
                alert('请填写所有必填字段！');
                return;
            }
            
            try {
                console.log('正在加载模型...');
                // 加载模型
                const model = await joblib.load('rf_classifier_model.pkl');
                console.log('模型加载成功');
                
                // 准备输入数据
                const inputData = [al, age, gender, al_cr, 0];
                console.log('输入数据:', inputData);
                
                // 进行预测
                const prediction = model.predict([inputData])[0];
                console.log('预测结果:', prediction === 1 ? '真性近视' : '非真性近视');
                
                // 显示结果
                const resultDiv = document.getElementById('result');
                resultDiv.style.display = 'block';
                
                if (prediction === 1) {
                    resultDiv.innerHTML = '<h3>预测结果: 真性近视</h3>';
                    resultDiv.style.backgroundColor = '#f8d7da';
                } else {
                    resultDiv.innerHTML = '<h3>预测结果: 非真性近视</h3>';
                    resultDiv.style.backgroundColor = '#d4edda';
                }
            } catch (error) {
                console.error('预测出错:', error);
                alert('预测过程中出现错误，请检查控制台日志');
            }
        }
    </script>
</body>
</html>